<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>tab2</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        ul {
            list-style-type: none;
        }

        .box {
            width: 400px;
            height: 300px;
            border: 1px solid #ccc;
            margin: 100px auto;
            overflow: hidden;
        }

        .hd {
            height: 45px;
        }

        .hd span {
            display: inline-block;
            width: 90px;
            background-color: pink;
            line-height: 45px;
            text-align: center;
            cursor: pointer;
        }

        .hd span.current {
            background-color: purple;
        }

        .bd div {
            height: 255px;
            background-color: purple;
            display: none;
        }

        .bd div.current {
            display: block;
        }
    </style>
</head>
<body>
<div class="box">
    <div class="hd" id="hd">
        <span class="current">体育</span>
        <span>娱乐</span>
        <span>新闻</span>
        <span>综合</span>
    </div>
    <div class="bd" id="bd">
        <div class="current">我是体育模块</div>
        <div>我是娱乐模块</div>
        <div>我是新闻模块</div>
        <div>我是综合模块</div>
    </div>
</div>
<script>
    //获取span和div
    var hd = document.getElementById("hd");
    var spans = hd.getElementsByTagName("span");
    var bd = document.getElementById("bd");
    var divs = bd.getElementsByTagName("div");
    //给所有的事件源绑定事件
    for (i = 0; i < spans.length; i++) {
        var span = spans[i];
        span.num = i;//给每个span绑定一个自定义属性
        // 触发事件时排他
        //排他  干掉所有人可以看错干掉span 同时一起干掉div
        span.onmouseover = function () {
            for (var i = 0; i < spans.length; i++) {
                var span = spans[i];
                var div = divs[i];
                span.className = "";
                div.className = "";
            }
            //干完别人保留span 鼠标经过的当前的那个自己
            this.className = "current"
            //怎样保留div 鼠标当前的div,可以看作是与span同步的 当前是this这个第i个span？
            var num = this.num;
            //divs是个伪数组 然后接受当前this这个第i个span 并同步把当前这个div保留
            divs[num].className = "current";
        }

    }
</script>
</body>
</html>